package com.example.demo.services.impl;

import com.example.demo.domain.Account;
import com.example.demo.persistence.AccountDao;
import com.example.demo.services.AccountService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@MapperScan("com.example.demo.persistence")
@MapperScan("com.example.demo.domain")
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    @Autowired
    private Account account;

    @Override
    public void updateAccount(Account account) throws SQLException {
        accountDao.updateAccount(account);
        accountDao.updateProfile(account);
        if(account.getPassword() != null && account.getPassword().length() > 0){
            accountDao.updateSignon(account);
        }
    }

    @Override
    public Account getAccount(String username){
        return accountDao.getAccountByUsername(username);
    }

    @Override
    public Account getAccount(String username, String password){
        account.setUsername(username);
        account.setPassword(password);
        return accountDao.getAccountByUsernameAndPassword(account);
    }

    @Override
    public void insertAccount(Account account) throws SQLException {
        accountDao.insertAccount(account);
        accountDao.insertProfile(account);
        accountDao.insertSignon(account);
    }

}
